package com.rc.evcharger.controller;

import com.google.gson.Gson;
import com.rc.evcharger.biz.utils.PubFetchTool;
import com.rc.evcharger.common.base.TResponse;
import com.rc.evcharger.common.constant.CommonConstant;
import com.rc.evcharger.common.utils.DataUtil;
import com.rc.evcharger.model.hlhtsk.HlhtskTenant;
import com.rc.evcharger.service.hlhtsk.HlhtskTenantService;
import com.rc.evcharger.vo.cec.StdRequest;
import com.rc.evcharger.vo.charge.CommandCurrDataReq;
import com.rc.evcharger.service.CurrDataService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 读取充电过程中数据Controller，不需解密
 * @author peter
 */
@RestController
@RequestMapping("/currcommand")
public class CurrDataController {
    private static Logger logger = LogManager.getLogger(CurrDataController.class);

    @Autowired
    private CurrDataService currDataService;

    @Autowired
    private HlhtskTenantService hlhtskTenantService;

    /**
     * 根据单项数据项编码获取数据
     * @param tRequest
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getCurrData", method = RequestMethod.POST)
    public TResponse getCurrData(@RequestBody StdRequest tRequest) {
        try {
            String tenantCode = tRequest.getSeq();  //Seq存储tenantCode
            String hlhtTenantCode = tRequest.getOperatorID(); //OperatorID存储hlhtTenantCode;

            HlhtskTenant hlhtskTenant = null;
            if (hlhtTenantCode == null) {//自充
                hlhtskTenant = hlhtskTenantService.selectHlhtTenantByTenantCode(tenantCode);
            } else {//内联充电
                hlhtskTenant = hlhtskTenantService.selectBetweenTenantCode(tenantCode, hlhtTenantCode);
            }

            //解密
            String deData = PubFetchTool.genDDDecodeData(hlhtskTenant, tRequest.getData());

            CommandCurrDataReq commandCurrDataReq = new Gson().fromJson(deData, CommandCurrDataReq.class);
            logger.info("/getCurrData 入参:{}", commandCurrDataReq.toString());

            if (commandCurrDataReq.getElecPileId() == null || StringUtils.isBlank(commandCurrDataReq.getGunNum())) {
                return TResponse.FAIL(CommonConstant.MSG_PARAMETER_EMPTY);
            }

            String[] codes = DataUtil.turnCode(commandCurrDataReq.getCode());
            if (codes == null) {
                return TResponse.FAIL("数据编码有误");
            }

            //调用指令
            String result = currDataService.getCurrData(commandCurrDataReq.getElecPileId(),
                    commandCurrDataReq.getMemberId(), commandCurrDataReq.getGunNum(), codes[0]);
            logger.info("getCurrData:{}", result);
            TResponse tResponse = TResponse.SUCCESS();
            tResponse.put(result);

            return tResponse;
        } catch (Exception e) {
            logger.error("getCurrData 异常", e);
            return TResponse.FAIL(CommonConstant.MSG_EXCEPTION);
        }
    }

    /**
     * 根据多项数据项编码获取数据
     * @param tRequest
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getCurrDataMap", method = RequestMethod.POST)
    public TResponse getCurrDataMap(@RequestBody StdRequest tRequest) {
        try {
            String tenantCode = tRequest.getSeq();  //Seq存储tenantCode
            String hlhtTenantCode = tRequest.getOperatorID(); //OperatorID存储hlhtTenantCode;

            HlhtskTenant hlhtskTenant = null;
            if (hlhtTenantCode == null) {//自充
                hlhtskTenant = hlhtskTenantService.selectHlhtTenantByTenantCode(tenantCode);
            } else {//内联充电
                hlhtskTenant = hlhtskTenantService.selectBetweenTenantCode(tenantCode, hlhtTenantCode);
            }

            //解密
            String deData = PubFetchTool.genDDDecodeData(hlhtskTenant, tRequest.getData());

            CommandCurrDataReq commandCurrDataReq = new Gson().fromJson(deData, CommandCurrDataReq.class);
            logger.info("/getCurrDataMap 入参:{}", commandCurrDataReq.toString());

            if (commandCurrDataReq.getElecPileId() == null || StringUtils.isBlank(commandCurrDataReq.getGunNum())) {
                return TResponse.FAIL(CommonConstant.MSG_PARAMETER_EMPTY);
            }
            String[] codes = DataUtil.turnCode(commandCurrDataReq.getCode());
            if (codes == null) {
                return TResponse.FAIL("数据编码有误");
            }

            //调用指令
            Map<String, Object> result = currDataService.getCurrData(commandCurrDataReq.getElecPileId(),
                    commandCurrDataReq.getMemberId(), commandCurrDataReq.getGunNum(), commandCurrDataReq.getCode());

            TResponse tResponse = TResponse.SUCCESS();
            tResponse.put(result);
            return tResponse;
        } catch (Exception e) {
            logger.error("getCurrDataMap 异常", e);
            return TResponse.FAIL(CommonConstant.MSG_EXCEPTION);
        }
    }

    /**
     * 读取SOC
     * @param tRequest
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getCurrDataSoc", method = RequestMethod.POST)
    public TResponse getCurrDataSoc(@RequestBody StdRequest tRequest) {
        try {
            String tenantCode = tRequest.getSeq();  //Seq存储tenantCode
            String hlhtTenantCode = tRequest.getOperatorID(); //OperatorID存储hlhtTenantCode;

            HlhtskTenant hlhtskTenant = null;
            if (hlhtTenantCode == null) {//自充
                hlhtskTenant = hlhtskTenantService.selectHlhtTenantByTenantCode(tenantCode);
            } else {//内联充电
                hlhtskTenant = hlhtskTenantService.selectBetweenTenantCode(tenantCode, hlhtTenantCode);
            }

            //解密
            String deData = PubFetchTool.genDDDecodeData(hlhtskTenant, tRequest.getData());
            CommandCurrDataReq commandCurrDataReq = new Gson().fromJson(deData, CommandCurrDataReq.class);
            logger.info("/getCurrDataSoc 入参:{}", commandCurrDataReq.toString());

            if (commandCurrDataReq.getElecPileId() == null || StringUtils.isBlank(commandCurrDataReq.getGunNum())) {
                return TResponse.FAIL(CommonConstant.MSG_PARAMETER_EMPTY);
            }
            String[] codes = DataUtil.turnCode(commandCurrDataReq.getCode());
            if (codes == null) {
                return TResponse.FAIL("数据编码有误");
            }

            //调用指令
            Map<String, Object> result = currDataService.getCurrSoc(commandCurrDataReq.getElecPileId(),
                    commandCurrDataReq.getGunNum(), commandCurrDataReq.getCode());

            TResponse tResponse = TResponse.SUCCESS();
            tResponse.put(result);
            return tResponse;
        } catch (Exception e) {
            logger.error("getCurrDataSoc 异常", e);
            return TResponse.FAIL(CommonConstant.MSG_EXCEPTION);
        }
    }

}
